package org.tis.tools.abf.module.ac.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.ac.controller.request.AcMenuFilterRequest;
import org.tis.tools.abf.module.ac.controller.request.AcMenuMoveRequest;
import org.tis.tools.abf.module.ac.controller.request.AcTreeQueryParameterRequest;
import org.tis.tools.abf.module.ac.entity.AcMenu;
import org.tis.tools.abf.module.ac.entity.vo.AcMenuDetail;
import org.tis.tools.abf.module.ac.entity.vo.AcMenuUiEntityDetail;
import org.tis.tools.abf.module.ac.service.IAcMenuService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * acMenu的Controller类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/acMenus")
@Validated
@Slf4j
public class AcMenuController extends BaseController<AcMenu>  {

    @Autowired
    private IAcMenuService acMenuService;


    @ApiOperation(value = "新增根菜单",notes = "传入AcMenu对象作为参数")
    @OperateLog(type = OperateType.ADD,desc = "新增根菜单")
    @PostMapping("/addAcMenu")
    public ResultVO addAcMenu(@RequestBody @Validated @ApiParam(value = "AcMenu",name = "传入的AcMenu对象",required = true) AcMenu acMenu) {
        AcMenu result = acMenuService.createRootMenu(acMenu);
        if(result == null){
            return ResultVO.error("422","新增菜单失败！");
        }
        return ResultVO.success("新增成功！",result);
    }


    @ApiOperation(value = "新增子菜单",notes = "传入AcMenu对象作为参数")
    @OperateLog(type = OperateType.ADD, desc = "添加子菜单")
    @PostMapping("/addSubAcmenu")
    public ResultVO addSubAcmenu(@RequestBody @Validated @ApiParam(value = "AcMenu",name = "传入的AcMenu对象",required = true) AcMenu acMenu) {
        AcMenu result = acMenuService.createChildMenu(acMenu);
        if(result == null){
            return ResultVO.error("422","新增菜单失败！");
        }
        return ResultVO.success("新增成功！",result);
    }


    @ApiOperation(value = "修改菜单",notes = "传入AcMenu对象作为参数")
    @OperateLog(type = OperateType.UPDATE, desc = "修改菜单")
    @PutMapping("/updateAcMenu")
    public ResultVO update(@RequestBody @Validated @ApiParam(value = "AcMenu",name = "传入的AcMenu对象",required = true) AcMenu acMenu) {
        AcMenu acMenuQue = acMenuService.selectById(acMenu.getGuid());
        if (acMenuQue == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        AcMenu result = acMenuService.updateAcMenu(acMenu);
        return ResultVO.success("修改成功！",result);
    }


    @ApiOperation(value = "删除菜单",notes = "传入菜单GUID作为参数")
    @OperateLog(type = OperateType.DELETE, desc = "删除菜单")
    @DeleteMapping("/deleteAcMenu/{id}")
    public ResultVO deleteAcMenu(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "传入菜单的GUID",required = true) String id) {
        AcMenu acMenu = acMenuService.selectById(id);
        if (acMenu == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        AcMenu result = acMenuService.deleteAllSubAcMenu(id);
        return ResultVO.success("删除成功" ,result);
    }


    @ApiOperation(value = "查询单条记录",notes = "传入菜单GUID作为参数")
    @GetMapping("/querySingAcMenu/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "传入菜单的GUID",required = true) String id) {
        AcMenu acMenu = acMenuService.selectById(id);
        if (acMenu == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", acMenuService.getDetail(id));
    }


    @ApiOperation(value = "分页查询所有菜单",notes = "传入菜单page,condition作为参数")
    @PostMapping("/queryPageAllList")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "传入菜单的page,condition",required = true) SmartPage<AcMenu> page) {
        return  ResultVO.success("查询成功", acMenuService.getPage(page.getPageObj(), page.getConditionWrapper()));
    }


    @ApiOperation(value = "查询应用下根菜单数据",notes = "传入应用GUID作为参数")
    @GetMapping("/queryAcMenuLists/{id}")
    public ResultVO getMenu(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "传入应用的GUID",required = true) String id){
        List lists = acMenuService.queryRootMenu(id);
        return ResultVO.success("查询成功",lists);
    }


    @ApiOperation(value = "过滤查询应用下菜单数据",notes = "传入AcMenuFilterRequest类作为参数")
    @PostMapping("/queryAcMenuFilterLists")
    public ResultVO queryAcMenuFilterLists(@RequestBody @Validated @ApiParam(value = "AcMenuFilterRequest",name = "传入的AcMenuFilterRequest类",required = true) AcMenuFilterRequest acMenuFilterRequest){
        AcMenuDetail acMenuDetail = acMenuService.queryAcMenuFilterLists(acMenuFilterRequest);
        return ResultVO.success("查询成功",acMenuDetail);
    }


    @ApiOperation(value = "根据父菜单GUID查询父菜单的下级子菜单",notes = "传入父菜单GUID作为参数")
    @GetMapping("/querySubAcMenuLists/{id}")
    public ResultVO getSubMenu(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "传入父菜单的GUID",required = true) String id){
        AcMenu acMenu = acMenuService.selectById(id);
        if (acMenu == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        List lists = acMenuService.selectSubMenu(id);
        return ResultVO.success("查询成功",lists);
    }


    @ApiOperation(value = "根据父菜单GUID和菜单名称模糊查询父菜单的下级子菜单",notes = "传入AcTreeQueryParameterRequest类作为参数")
    @PostMapping("/getSubMenuFilterList")
    public ResultVO getSubMenuFilter(@RequestBody @Validated @ApiParam(value = "AcTreeQueryParameterRequest",name = "传入的AcTreeQueryParameterRequest类",required = true) AcTreeQueryParameterRequest parameterRequest){
        AcMenu acMenu = acMenuService.selectById(parameterRequest.getGuidParents());
        if (acMenu == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        List<AcMenuUiEntityDetail> lists = acMenuService.getSubMenuFilter(parameterRequest.getGuidParents(),parameterRequest.getName());
        return ResultVO.success("查询成功",lists);
    }


    @ApiOperation(value = "移动菜单",notes = "传入AcMenuMoveRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE, desc = "修改菜单（移动）")
    @PostMapping("/queryMoveMenuLists")
    public ResultVO getMoveMenu(@RequestBody @Validated @ApiParam(value = "AcMenuMoveRequest",name = "传入的AcMenuMoveRequest类",required = true) AcMenuMoveRequest acMenuMoveRequest){

        AcMenu lists = acMenuService.moveMenu(acMenuMoveRequest.getTargetGuid(),acMenuMoveRequest.getMoveGuid(),
                acMenuMoveRequest.getOrder());
        return ResultVO.success("查询成功",lists);
    }
}

